<template>
    <div class="login-box">
        <div class="field">
            <mt-field label="旧密码" placeholder="请输入旧密码" type="password" v-model="oldpassword"></mt-field>
            <mt-field label="密码" placeholder="请输入新密码" type="password" v-model="password"></mt-field>
            <mt-field label="确认密码" placeholder="请再次输入密码" type="password" v-model="password2"></mt-field>
        </div>
        <div class="btn">
            <mt-button type="primary" class="normal" :disabled="logining" @click="submit">
                <div class="btn-box">
                    <span v-if="!logining">修改</span>
                    <mt-spinner type="triple-bounce" color="white" v-if="logining"></mt-spinner>
                </div>
            </mt-button>
        </div>
    </div>
</template>

<script>
    import { Toast } from 'mint-ui'
    import apiUrl from '@/api/apiUrl'

    export default {
        name: 'index',
        data () {
            return {
                logining: false,
                oldpassword: '',
                password: '',
                password2: '',
            }
        },
        methods: {
            submit () {
                if (!this.oldpassword) {
                    return Toast('旧密码不能为空')
                }

                if (!this.password) {
                    return Toast('密码不能为空')
                }

                if (this.password != this.password2) {
                    return Toast('两次密码不相同')
                }

                this.loading = true

                let params = new URLSearchParams();
                    params.append('oldpassword', this.oldpassword)
                    params.append('password', this.password)

                this.$http.post(apiUrl.api.editPassword, params).then(res => {
                    if (res.data.code === 0) {
                        Toast('修改成功')
                    } else {
                        Toast(res.data.msg)
                    }
                    this.oldpassword = ''
                    this.password = ''
                    this.loading = false
                }).catch(err => {
                    console.log(err)
                    this.oldpassword = ''
                    this.password = ''
                    this.loading = false
                })
            },
        }
    }
</script>

<style scoped lang="less">
    @import '../../../assets/css/bases';
    .login-box {
        .field {
            .vcode {
                border: none;
                background: white;
                outline: none;
                font-size: @default;
            }
        }
        .btn {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: @lager;
            width: 100%;
            .normal {
                width: 80%;
                .btn-box {
                    width: 100%;
                    height: 100%;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }
            }
        }
        .more {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: @middle;
            width: 100%;
            .link {
                width: 80%;
                a {
                    color: @error;
                }
            }
        }
    }
</style>